package com.creawor.hz_market.t_infotype;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.hibernate.HibernateException;

import com.creawor.imei.base.Common_qry_QueryMap;

public class InfoTypeManager {
	private static final String SELECT_ALL_INFOTYPE = "select * from info_type ";
	private static final String SELECT_ALL_INFOTYPEUSER = "select * from info_type_super ";
	private static final String SELECT_ALL_INFOTYPEUSER_DISTINCT = "select distinct user_id from info_type_super ";
	private static final String SELECT_USER_DEPT_ROLE = "select distinct * from t_user a, t_user_role b where a.id=b.user_id ";
	private static final String SELECT_USERID_DEPT_ROLE = "select distinct a.id from t_user a, t_user_role b where a.id=b.user_id ";
	private static final String SELECT_USER_BYID = "select * from t_user where id in ";
	private static final String SELECT_USER_WRITER = "select * from t_user";

	private static List INFO_TYPE_LIST = null;

	// info_type
	public List findAllInfoType() {
		// List allit = null;
		if (INFO_TYPE_LIST == null) {
			try {
				Common_qry_QueryMap query_map0 = new Common_qry_QueryMap();
				INFO_TYPE_LIST = query_map0.findAll(SELECT_ALL_INFOTYPE);
			} catch (HibernateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			// return INFO_TYPE_LIST;
		}

		return INFO_TYPE_LIST;
	}

	public Map findAllInfoTypeMap() {
		if (INFO_TYPE_LIST == null) {
			findAllInfoType();
		}
		Map infomap = new HashMap();
		for (int i = 0; i < INFO_TYPE_LIST.size(); i++) {
			Map m = (Map) INFO_TYPE_LIST.get(i);
			infomap.put((String) m.get("id"), m);
		}
		return infomap;
	}

	public String InfoTypeStr() {
		StringBuffer str = new StringBuffer("");
		List all = findAllInfoType();

		if (all != null) {
			for (int i = 0; i < all.size(); i++) {
				Map map = (Map) all.get(i);
				if (i != 0) {
					str.append(",");
				}
				str.append((String) map.get("id"));
				str.append(":");
				str.append((String) map.get("type_name"));
			}
		}
		return str.toString();
	}

	public String InfoTypeStr(int pid) {
		StringBuffer str = new StringBuffer("");
		List all = findInfoTypeByPid(pid);

		if (all != null) {
			for (int i = 0; i < all.size(); i++) {
				Map map = (Map) all.get(i);
				if (i != 0) {
					str.append(",");
				}
				str.append((String) map.get("id"));
				str.append(":");
				str.append((String) map.get("type_name"));
			}
		}
		return str.toString();
	}

	public String InfoTypeStr(String pid) {
		if (pid == null) {
			return "";
		}
		return InfoTypeStr(Integer.parseInt(pid));

	}

	public Map findInfoTypeByid(int id) {
		Map map = null;
		Map allmap = findAllInfoTypeMap();
		map = (Map) allmap.get(String.valueOf(id));
		if (map == null) {
			List allit = null;
			try {
				Common_qry_QueryMap query_map0 = new Common_qry_QueryMap();
				allit = query_map0.findAll(SELECT_ALL_INFOTYPE + " where id = "
						+ id);
			} catch (HibernateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (allit != null && allit.size() > 0) {
				map = (Map) allit.get(0);
			}
		}
		return map;
	}

	public String getInfoTypeNameStr(int id, String s) {
		String str = "";
		Map map = findInfoTypeByid(id);
		if (map != null) {
			String idstr = (String) map.get("id");
			String pidstr = (String) map.get("pid");
			String type_name = (String) map.get("type_name");
			if (getInfoTypeNameStr(Integer.parseInt(pidstr), str) != null) {
				str = getInfoTypeNameStr(Integer.parseInt(pidstr), str) + "-"
						+ type_name;
			} else {
				str += type_name;
			}
		} else {
			return null;
		}
		return str;
	}

	public String getInfoTypeNameStr(String id, String s) {
		String str = "";
		try {
			str = getInfoTypeNameStr(Integer.parseInt(id), s);
		} catch (Exception e) {
			return id;
		}
		return str;
	}

	public List findInfoTypeByPid(int pid) {
		List allit = null;
		try {
			Common_qry_QueryMap query_map0 = new Common_qry_QueryMap();
			allit = query_map0.findAll(SELECT_ALL_INFOTYPE + " where pid = "
					+ pid);
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return allit;
	}

	public int findInfoTypePidByid(int id) {
		Map map = findInfoTypeByid(id);
		if (map != null) {
			// Integer integ = (Integer) map.get("pid");
			int integ = Integer.parseInt((String) map.get("pid"));
			return integ;
		} else {
			return -1;
		}
	}

	public Map findPInfoTypeByid(int id) {
		int in = findInfoTypePidByid(id);
		return findInfoTypeByid(in);
	}

	// info_type_super
	public List findAllInfoTypeUser() {
		List allit = null;
		try {
			Common_qry_QueryMap query_map0 = new Common_qry_QueryMap();
			allit = query_map0.findAll(SELECT_ALL_INFOTYPEUSER);
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return allit;
	}

	public List findInfoTypeUserByUserid(int uid) {
		List allit = null;
		try {
			Common_qry_QueryMap query_map0 = new Common_qry_QueryMap();
			allit = query_map0.findAll(SELECT_ALL_INFOTYPEUSER
					+ " where user_id = " + uid);
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return allit;
	}

	public List findInfoTypeUserByTypeid(int tid) {
		List allit = null;
		try {
			Common_qry_QueryMap query_map0 = new Common_qry_QueryMap();
			allit = query_map0.findAll(SELECT_ALL_INFOTYPEUSER
					+ " where info_type = " + tid);
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return allit;
	}

	public Map findPermissionInfoTypeidMapByUid(int uid) {
		List pinfo = new ArrayList();
		Map typeidmap = new HashMap();
		List p = findInfoTypeUserByUserid(uid);
		if (p == null) {
			return null;
		} else {
			for (int i = 0; i < p.size(); i++) {
				Map map = (Map) p.get(i);
				// int typeid = ((Integer) map.get("info_type")).intValue();
				int typeid = Integer.parseInt((String) map.get("info_type"));
				if (!typeidmap.containsKey(String.valueOf(typeid))) {
					Map m = findInfoTypeByid(typeid);
					pinfo.add(m);
					findChildPermission1(typeidmap, pinfo, typeid);
				}
				typeidmap.put(String.valueOf(typeid), null);

			}
		}
		// System.out.println(pinfo);
		return typeidmap;
	}

	public Map findPermissionInfoTypeidMapBytypeid(int tid) {
		List pinfo = new ArrayList();
		Map typeidmap = new HashMap();

		// int typeid = ((Integer) map.get("info_type")).intValue();
		int typeid = tid;
		if (!typeidmap.containsKey(String.valueOf(typeid))) {
			Map m = findInfoTypeByid(typeid);
			pinfo.add(m);
			findChildPermission1(typeidmap, pinfo, typeid);
		}
		typeidmap.put(String.valueOf(typeid), null);

		return typeidmap;
	}

	public List findPermissionInfoTypeByUid(int uid) {
		List pinfo = new ArrayList();
		Map typeidmap = new HashMap();
		List p = findInfoTypeUserByUserid(uid);
		if (p == null) {
			return null;
		} else {
			for (int i = 0; i < p.size(); i++) {
				Map map = (Map) p.get(i);
				// int typeid = ((Integer) map.get("info_type")).intValue();
				int typeid = Integer.parseInt((String) map.get("info_type"));
				if (!typeidmap.containsKey(typeid)) {
					Map m = findInfoTypeByid(typeid);
					pinfo.add(m);
					findChildPermission(typeidmap, pinfo, typeid);
				}
				typeidmap.put(typeid, null);

			}
		}
		// System.out.println(pinfo);
		return pinfo;
	}

	private void findChildPermission(Map typeidmap, List pinfo, int pid) {
		List clist = findInfoTypeByPid(pid);
		if (clist != null && clist.size() > 0) {
			for (int i = 0; i < clist.size(); i++) {
				Map map = (Map) clist.get(i);
				// int typeid = ((Integer) map.get("id")).intValue();
				int typeid = Integer.parseInt((String) map.get("id"));
				if (!typeidmap.containsKey(typeid)) {
					Map m = findInfoTypeByid(typeid);
					pinfo.add(m);
				}
				typeidmap.put(typeid, null);
				findChildPermission(typeidmap, pinfo, typeid);
			}
		}
	}

	private void findChildPermission1(Map typeidmap, List pinfo, int pid) {
		List clist = findInfoTypeByPid(pid);
		if (clist != null && clist.size() > 0) {
			for (int i = 0; i < clist.size(); i++) {
				Map map = (Map) clist.get(i);
				// int typeid = ((Integer) map.get("id")).intValue();
				int typeid = Integer.parseInt((String) map.get("id"));
				if (!typeidmap.containsKey(String.valueOf(typeid))) {
					Map m = findInfoTypeByid(typeid);
					pinfo.add(m);
				}
				typeidmap.put(String.valueOf(typeid), null);
				findChildPermission1(typeidmap, pinfo, typeid);
			}
		}
	}

	public List findUsers() {
		List allit = null;
		try {
			Common_qry_QueryMap query_map0 = new Common_qry_QueryMap();
			allit = query_map0.findAll(SELECT_ALL_INFOTYPEUSER_DISTINCT);
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return allit;
	}

	public List findUseridsByTypeid(String tid) {
		if(tid==null||"".equals(tid)){
			return null;
		}
		List userlist = findUsers();
		if (userlist == null) {
			return null;
		}
		List ulist = new ArrayList();
		for (int i = 0; i < userlist.size(); i++) {
			Map m = (Map) userlist.get(i);
			String uid = (String) m.get("user_id");
			Map userpmismaqp = findPermissionInfoTypeidMapByUid(Integer
					.parseInt(uid));
			if (userpmismaqp.containsKey(tid)) {
				ulist.add((String)m.get("user_id"));
			}
		}
		return ulist;
	}

	public List findUsersByDeptAndRole(String dept, String roleid) {
		List allit = null;
		try {
			Common_qry_QueryMap query_map0 = new Common_qry_QueryMap();
			allit = query_map0.findAll(SELECT_USER_DEPT_ROLE
					+ " and a.dept_code='" + dept + "' and b.role_id='"
					+ roleid + "' and mobile is not null");
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return allit;
	}

	public List findUsersByWriter(String writer) {
		List allit = null;
		try {
			Common_qry_QueryMap query_map0 = new Common_qry_QueryMap();
			allit = query_map0.findAll(SELECT_USER_WRITER
					+ "  where usercode='" + writer + "' and mobile is not null");
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return allit;
	}

	public List findUseridsByDeptAndRole(String dept, String roleid) {
		List allit = null;
		try {
			Common_qry_QueryMap query_map0 = new Common_qry_QueryMap();
			allit = query_map0.findAll(SELECT_USERID_DEPT_ROLE
					+ " and a.dept_code='" + dept + "' and b.role_id='"
					+ roleid + "'");
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return allit;
	}

	/**
	 * @param tid
	 * @return
	 */
	public String[] findUserMobiles(String tid) {
		String[] s = null;
		List l = findUseridsByTypeid(tid);
		if (l == null) {
			return null;
		}
		if (l.size()<1) {
			return null;
		}
		String sqlstr = "(";
		for (int i = 0; i < l.size(); i++) {
			if (i != 0) {
				sqlstr += ",";
			}
			sqlstr += "'" + l.get(i) + "'";

		}
		sqlstr += ")";

		sqlstr = SELECT_USER_BYID + sqlstr + " and mobile is not null";
		Common_qry_QueryMap query_map0;
		try {
			query_map0 = new Common_qry_QueryMap();
			List allit = query_map0.findAll(sqlstr);
			if (allit != null) {
				s = new String[allit.size()];
				for (int j = 0; j < allit.size(); j++) {
					Map m = (Map) allit.get(j);
					s[j] = (String) m.get("mobile");
				}
			}

		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return s;
	}

	public String[] findUserMobiles(String dept, String roleid) {
		String[] s = null;
		List l = findUsersByDeptAndRole(dept, roleid);
		if (l != null) {
			s = new String[l.size()];
			for (int j = 0; j < l.size(); j++) {
				Map m = (Map) l.get(j);
				s[j] = (String) m.get("mobile");
			}
		}
		return s;
	}

	public static void main(String[] arg0) {
		InfoTypeManager im = new InfoTypeManager();
		System.out.println(im.findUserMobiles("11"));
	}

}
